'''


字典推导式

'''

dict1 = {i: i**2 for i in range(1, 5)}
print(dict1)

# 两个列表组合成字典
list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, 'man']
dict2 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict2)


# 两个列表组合成字典, 条件判断
list3 = ['name', 'age', 'gender']
list4 = ['Tom', 20, 'man']
dict3 = {list3[i]: list4[i] for i in range(len(list3)) if i % 2 == 0}
print(dict3)

# 两个列表组合成字典, 列表长度不一致, 只取长度短的
list4 = ['name', 'age', 'gender','adress','hoppies']
list5 = ['Tom', 20, 'man','beijing','swim','run']
dict4 = {list4[i]: list5[i] for i in range(min(len(list4), len(list5)))}
print(dict4)


# 提取字典中的键和值
counts = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50, 'f': 60, 'g': 70}
dict5 = {key: value for key, value in counts.items() if value >= 40}
print(dict5)

# 总结，始终是循环操作，所以都是for
for key, value in counts.items():
    if value >= 40:
        print(key, value)
    # print(key, value)

print(counts.items())
# 从字典提取数据，并构建元祖
dict6 = [(k, v) for k,v in counts.items() if v >= 50]
print(dict6)
